package top.kaoshanji.p1ch03c;

import org.apache.hc.core5.http.ContentType;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;

/**
 * 代码清单-p1ch03c-01：资源API示例
 * @author kaoshanji
 * ^_^
 * create time 2025/5/25 14:47
 */
@WebServlet(urlPatterns = {"/hi"})
public class HiServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 设置请求对象的编码
        req.setCharacterEncoding(StandardCharsets.UTF_8.name());

        String token = req.getParameter("token");

        System.out.println("...客户端发送来的Token..."+token);

        // 设置响应对象的编码
        resp.setContentType(ContentType.APPLICATION_JSON.toString());
        PrintWriter out = resp.getWriter();
        out.write("我是靓仔..你记住！");

    }

}
